home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3528 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.8 KB  |  200 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: New C Programmer Has A Problem
  5. Message-ID: <1996Jan29.162514.3498@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4ehpa3$6kl@nntp.novia.net>
  11. Date: Mon, 29 Jan 1996 16:25:14 GMT
  12.  
  13. In article <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
  14. >  I have a question on C... why isn't this program working?!
  15.  
  16. What are the detailed symptoms? Are they compilation or link or
  17. run-time errors?
  18.  
  19. >  Program START:
  20. >  /* File Open */
  21. >  
  22. >  #include <clib/dos_protos.h>
  23. >  #include <dos/dos.h>
  24. >  #include <stdio.h>
  25. >  #include <exec/types.h>
  26.  
  27. The above includes are mostly non-standard (with the exception of
  28. stdio.h) as far as I can tell, so I am not sure if they are correct or
  29. not.
  30.  
  31. >  void main();
  32.  
  33. This above declaration of what main returns is unnecessary and also
  34. wrong per ANSI requirements (see later correction).
  35.  
  36. >    char *name;
  37. >    int age;
  38.  
  39. Effectively, the above two variables have become global to the program.
  40. In this particular case, they could be inside the main function with
  41. similar results.
  42.  
  43. >  void main()
  44.  
  45. Instead of the above, please use (see note earlier):
  46.  
  47.    int main()
  48.  
  49. >  
  50. >  {
  51. >    struct FileHandle *file_handle;
  52.  
  53. I do not know what the above structure is. Why not just use the FILE
  54. pointer declaration as follows:
  55.  
  56.      FILE *file_handle;
  57.  
  58. Then you can use the more easy fopen, fclose, fread, fwrite, fprintf,
  59. etc., functions to deal with data in the file.
  60.  
  61. >    long bytes_written;
  62. >    long bytes_read;
  63. >  
  64. >   printf("Enter your name: ");
  65. >   scanf("%s",name);
  66.  
  67. This line above is one of the places where you should expect problems
  68. (there are others in your code, but this is one of the common errors).
  69.  
  70. The comp.lang.c FAQ talks about this issue (allocating a pointer but
  71. not initializing it to point to any valid memory before you attempt to
  72. store data into that unassigned location).
  73.  
  74. Specifically, section 7 (Memory Allocation) in the FAQ should be read
  75. thoroughly by you before proceeding.
  76.  
  77. >   printf("\nEnter your age: ");  scanf("%d",age);
  78. >  
  79. >  
  80. >    file_handle=(struct FileHandle *)
  81. >      Open("RAM:You.dat",MODE_NEWFILE);
  82.  
  83. As mentioned earlier, "fopen" is probably more easy to use. If declared
  84. as I had mentioned earlier, the above line would become:
  85.  
  86.      file_handle = fopen("RAM:You.dat","w");
  87.  
  88. >    /* Have we opened the file successfully? */
  89. >    if(file_handle==NULL)
  90. >    {
  91. >      printf("Could not open the file!\n");
  92. >      Exit(0);
  93.  
  94. If main() is declared to be int return, then the above could be:
  95.  
  96.         return(0);
  97.  
  98. although it is more common to return 0 to imply success, so it might
  99. be better to use:
  100.  
  101.         return(1);
  102.  
  103. or something more appropriate if you want to trap the result of the
  104. return outside the program.
  105.  
  106. >    }
  107. >    /* We have now opened a file, and are ready to start writing: */
  108. >    bytes_written=Write(file_handle,name,sizeof(name));
  109.  
  110. What is the "Write" function? It is not one of the standard library
  111. functions I am familiar with.
  112.  
  113. >    bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
  114.  
  115. Some simplification (using a technique in C that is quite common) will
  116. make the above line read as follows (even though it may be incorrect
  117. code since I am not aware of what "Write" does):
  118.  
  119.      bytes_written += Write(file_handle,age,sizeof(age));
  120.  
  121. >  
  122. >    if(bytes_written!=sizeof(name)+sizeof(age))
  123. >    {
  124. >      printf("Could not save the list!\n");
  125. >      Close(file_handle);
  126. >      Exit(0);
  127. >    }
  128. >    else
  129. >      printf("Saved successfully!\n");
  130. >    printf("Memory cleared!\n");
  131. >     name="";
  132.  
  133. Oops. You may have difficulty with the above ... the pointer "name" will
  134. get set to point to another location other than allocated char array
  135. space (after you fix the other error).
  136.  
  137. >     age=0;
  138. >    printf("Loading!\n");
  139. >    Seek(file_handle,0,OFFSET_BEGINNING);
  140.  
  141. Is "Seek" a valid library function?
  142.  
  143. >    bytes_read=Read(file_handle,name,sizeof(name));
  144.  
  145. The function name "Read" above is unknown to me.
  146.  
  147. >    bytes_read=bytes_read+Read(file_handle,age,sizeof(age));
  148.  
  149. Try (although see note above about the function "Read"):
  150.  
  151.      bytes_read += Read(file_handle,age,sizeof(age));
  152.  
  153. >    if(bytes_written!=sizeof(name)+sizeof(age))
  154. >    {
  155. >      printf("Could not read the list!\n");
  156. >      Close(file_handle);
  157. >      Exit(0);
  158. >    }
  159.  
  160. Your error handling (using the number of bytes read) for determiningg
  161. that the data was not read correctly is not a cleanly consistent method
  162. I would recommend, although it probably will work in this case.
  163.  
  164. >    /* Print out the data: */
  165. >      printf("Hello, %s!\",name);
  166.                           ^
  167. Are you missing something here?
  168.  
  169. >      printf("You are %d years old!\n",age);
  170. >    /* Close the file: */
  171. >    Close(file_handle);
  172. >  }
  173. >  
  174. >  Program END:
  175. >  
  176. >  Any help if MUCH appreciated...
  177. > I am using the SAS/C 6.50 compiler...
  178.  
  179. Well, in a number of places, your compiler should have complained about
  180. incorrect code, or incorrect library functions (possibly - I do not
  181. know the SAS compiler). Did this even compile and link correctly? If
  182. so, I would recommend switching compilers <smile>. Other wise, I would
  183. ask what symptoms you specifically experienced, and what you tried to
  184. fix, so that we can address how you should progress next.
  185.  
  186. >  Also, is there any of you whom might have a routine to figure up random (or
  187. >  even psuedo-random) numbers?? Some algorithm, or such, as I need one BADLY!!
  188.  
  189. Please look in the comp.lang.c FAQ for answers to the above question!
  190.  
  191.                                 Z
  192.  
  193.  
  194. -- 
  195. -------------------------------------------------------------------------
  196. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  197. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  198. -------------------------------------------------------------------------
  199.